package smlab.effects
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 
	 * @author simon
	 * original author:pig-hong
	 * 
	 */
	public class Snow extends Sprite
	{
		private var snow:MovieClip;
		private var _xRange:Number;
		private var _baseYSpeed:Number;
		private var _snowYRange:Number;
		private var _xAngle:Number = 0;
		private var p:Number = 0;
		private var _scaleMin:Number;
		private var _scaleMix:Number;
		
		/**
		 * 
		 * @param xRange		雪花在X轴左右飘动的范围
		 * @param xAngle		雪花在X轴上下浮动的角度值
		 * @param baseYSpeed	雪花下落速度基础值
		 * @param snowYRange	雪花最大下落距离
		 * @param scaleMin		雪花最小缩放值
		 * @param scaleMix		雪花最大缩放值
		 * 
		 */
		public function Snow(xRange:Number=2,xAngle:Number=0.1,baseYSpeed:Number=2.5,snowYRange:Number=580,scaleMin:Number=0.8,scaleMix:Number=1.1)
		{
			super();
//			snow = new snow_mc();//可以使用你自己的雪花皮肤
			
			snow=new MovieClip;
			snow.graphics.beginFill(0xffffff);
			snow.graphics.drawCircle(0,0,10);
			
			this.cacheAsBitmap = true;
			this.addChild(snow);
			this.mouseChildren = false;
			this.mouseEnabled = false;
			_scaleMin=scaleMin;
			_scaleMix=scaleMix;
			this.scaleX = this.scaleY = scaleMin+Math.random()*(scaleMix-scaleMin);
			this.alpha = this.scaleX; 
			_xRange=xRange;
			_xAngle=xAngle;
			_baseYSpeed=baseYSpeed;
			_snowYRange=snowYRange;
			
			this.addEventListener(Event.ENTER_FRAME,onEnter);
		}
		private function onEnter(event:Event):void
		{
			p+=_xAngle;
			this.x+=Math.sin(p)*_xRange;
			this.y+=Math.random()+_baseYSpeed;
			
			if(this.y > _snowYRange)
			{
				destroy();
			}
		}
		public function destroy():void
		{
			this.removeEventListener(Event.ENTER_FRAME,onEnter);
			
			if(this.parent){
				this.parent.removeChild(this);
			}
			
			snow = null;
		}
	}
}

/*
用法示例（需配合 SnowController 使用）：
var sc:SnowController=new SnowController(stage);
sc.start();
*/